package com.alan.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * Created by Alan on 2017/9/29.
 */
public class WcMapper extends Mapper<LongWritable,Text,Text,IntWritable> {

    /**
     * 每次调用map方法会传入split中一行数据
     * @param key       该行数据所在文件中的位置下标
     * @param value     这行行数值
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //默认为空格split
        StringTokenizer st = new StringTokenizer(value.toString());
        while (st.hasMoreTokens()){
            String world = st.nextToken();
            context.write(new Text(world),new IntWritable(1));//map的输出
        }
    }
}
